package com.mail.controller;

import com.mail.common.DataResult;
import com.mail.dto.QuartzEmailDto;
import com.mail.enums.ResponseCode;
import com.mail.service.MailService;
import com.mail.service.QuartzJobService;
import com.yang.yangutils.utils.YangUtils;
import org.quartz.CronExpression;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/email/quartz")
public class QuartzJobController {
    @Resource
    private QuartzJobService quartzJobService;

    /**
     * 添加调度任务
     */
    @PostMapping("/saveJob")
    public DataResult saveJob(QuartzEmailDto quartzEmailDto){
        if(!YangUtils.CronValidIdentify(quartzEmailDto.getCron())){
            return DataResult.error(ResponseCode.CRON_IS_NOT_VALID.getMsg());
        }
        return quartzJobService.saveJob(quartzEmailDto);
    }

    /**
     * 重新定制定时任务
     */
    @PutMapping("/rescheduleJob")
    public DataResult rescheduleJob(String mailId, String cron){
        if(!YangUtils.CronValidIdentify(cron)){
            return DataResult.error(ResponseCode.CRON_IS_NOT_VALID.getMsg());
        }
        quartzJobService.rescheduleJob(mailId,cron);
        return DataResult.success();
    }

    /**
     * 暂停定时任务
     */
    @PostMapping("/pauseJob")
    public DataResult pauseJob(String mailId){
        quartzJobService.pauseJob(mailId);
        return DataResult.success();
    }

    /**
     * 恢复定时任务
     */
    @PostMapping("/resumeJob")
    public DataResult resumeJob(String mailId){
        quartzJobService.resumeJob(mailId);
        return DataResult.success();
    }

    /**
     * 删除定时任务
     */
    @DeleteMapping("/deleteJob")
    public DataResult deleteJob(String mailId){
        quartzJobService.deleteJob(mailId);
        return DataResult.success();
    }

    /**
     * 查询用户下的定时任务
     */
    @GetMapping("/listJobsByGroupName")
    public DataResult listJobsByGroupName(String groupName){
        return quartzJobService.listJobsByGroupName(groupName);
    }
}
